package com.car.loan.project.cm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.enums.SwitchStatus;
import com.car.loan.common.enums.UserStatus;
import com.car.loan.common.utils.BeanUtils;
import com.car.loan.common.utils.DateUtils;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.project.cm.domain.CmDealer;
import com.car.loan.project.cm.domain.CmDealerAccount;
import com.car.loan.project.cm.domain.CmInsuranceCompany;
import com.car.loan.project.cm.mapper.CmInsuranceCompanyMapper;
import com.car.loan.project.cm.service.ICmInsuranceCompanyService;
import com.car.loan.project.cm.util.select.InsuranceBaseSelect;
import com.car.loan.project.cm.util.vo.SelectedVo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 保险公司Service业务层处理
 *
 * @author ruoyi
 * @date 2019-10-19
 */
@Service
public class CmInsuranceCompanyServiceImpl implements ICmInsuranceCompanyService {

    @Autowired
    private CmInsuranceCompanyMapper cmInsuranceCompanyMapper;

    /**
     * 查询保险公司
     *
     * @param id 保险公司ID
     * @return 保险公司
     */
    @Override
    public CmInsuranceCompany selectCmInsuranceCompanyById(Integer id) {
        return cmInsuranceCompanyMapper.selectById(id);
    }

    /**
     * 查询保险公司列表
     *
     * @param cmInsuranceCompany 保险公司
     * @return 保险公司
     */
    @Override
    public List<CmInsuranceCompany> selectCmInsuranceCompanyList(
        CmInsuranceCompany cmInsuranceCompany) {
        QueryWrapper<CmInsuranceCompany> queryWrapper = new QueryWrapper<>(cmInsuranceCompany);
        return cmInsuranceCompanyMapper.selectList(queryWrapper);
    }

    /**
     * 新增保险公司
     *
     * @param cmInsuranceCompany 保险公司
     * @return 结果
     */
    @Override
    public int insertCmInsuranceCompany(CmInsuranceCompany cmInsuranceCompany) {
        cmInsuranceCompany.setCreateTime(DateUtils.getNowDate());
        return cmInsuranceCompanyMapper.insert(cmInsuranceCompany);
    }

    @Override
    public IPage<CmInsuranceCompany> selectPage(IPage<CmInsuranceCompany> page,
        QueryWrapper<CmInsuranceCompany> queryWrapper) {
        return cmInsuranceCompanyMapper.selectPage(page, queryWrapper);
    }

    @Override
    public IPage<SelectedVo> selectedPage(InsuranceBaseSelect insuranceBaseSelect) {
        IPage<CmInsuranceCompany> page = new Page<>(insuranceBaseSelect.getPageNum(),
            insuranceBaseSelect.getPageSize());
        QueryWrapper<CmInsuranceCompany> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(insuranceBaseSelect.getName())) {
            queryWrapper.like("alias", insuranceBaseSelect.getName());
        }
        queryWrapper.select("id,alias,status");
        if (null == insuranceBaseSelect.getOf() || SwitchStatus.OPEN.status == insuranceBaseSelect
            .getOf()) {
            queryWrapper.eq("status", UserStatus.OK.getCode());
        } else {
            queryWrapper.in("status", UserStatus.OK.getCode(), UserStatus.DISABLE.getCode());
        }
        queryWrapper.orderByDesc("id");
        IPage<CmInsuranceCompany> dealerIPage = cmInsuranceCompanyMapper
            .selectPage(page, queryWrapper);
        List<CmInsuranceCompany> records = dealerIPage.getRecords();
        IPage<SelectedVo> result = new Page<>();
        BeanUtils.copyBeanProp(result, dealerIPage);
        List<SelectedVo> selectedVos = new ArrayList<>();
        if (StringUtils.isNotEmpty(records)) {
            records.forEach(cmDealer -> {
                selectedVos.add(new SelectedVo().setValue(String.valueOf(cmDealer.getId()))
                    .setLabel(cmDealer.getAlias()).setStatus(cmDealer.getStatus()));
            });
        }
        result.setRecords(selectedVos);
        return result;
    }

    /**
     * 修改保险公司
     *
     * @param cmInsuranceCompany 保险公司
     * @return 结果
     */
    @Override
    public int updateCmInsuranceCompany(CmInsuranceCompany cmInsuranceCompany) {
        return cmInsuranceCompanyMapper.updateById(cmInsuranceCompany);
    }

    /**
     * 删除保险公司对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCmInsuranceCompanyByIds(Collection<Integer> ids) {
        return cmInsuranceCompanyMapper.deleteBatchIds(ids);
    }

    /**
     * 删除保险公司信息
     *
     * @param id 保险公司ID
     * @return 结果
     */
    @Override
    public int deleteCmInsuranceCompanyById(Integer id) {
        return cmInsuranceCompanyMapper.deleteById(id);
    }
}
